package com.ruoyi.project.dict.dictspider.service.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.project.dict.dictspider.domain.ZdicSpider;
import com.ruoyi.project.dict.dictspider.mapper.ZdicSpiderMapper;
import com.ruoyi.project.dict.dictspider.service.IZdicSpiderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 汉典内容Service业务层处理
 *
 * @author shixiangwen
 * @date 2021-03-05
 */
@Service
@DataSource(DataSourceType.DICT)
public class ZdicSpiderServiceImpl implements IZdicSpiderService {

    @Autowired
    private ZdicSpiderMapper zdicSpiderMapper;

    @Autowired
    @Qualifier("ssdbTemplate")
    RedisTemplate<String, String> redisTemplate;


    /**
     * 查询汉典内容
     *
     * @param id 汉典内容ID
     * @return 汉典内容
     */
    @Override
    public ZdicSpider selectZdicSpiderById(Long id) {
        ZdicSpider zdicSpider = zdicSpiderMapper.selectZdicSpiderById(id);
        zdicSpider.setZi(new String(zdicSpider.getZiBytes()));
        String kxzd = (String) redisTemplate.opsForHash().get("kxzd", zdicSpider.getZi());
        String syjz = (String) redisTemplate.opsForHash().get("syjz", zdicSpider.getZi());
        String gycd = (String) redisTemplate.opsForHash().get("gycd", zdicSpider.getZi());
        zdicSpider.setKxzdFmt(kxzd);
        zdicSpider.setSyjzFmt(syjz);
        zdicSpider.setGycdFmt(gycd);
        return zdicSpider;
    }

    /**
     * 查询汉典内容列表
     *
     * @param zdicSpider 汉典内容
     * @return 汉典内容
     */
    @Override
    public List<ZdicSpider> selectZdicSpiderList(ZdicSpider zdicSpider) {
        List<ZdicSpider> list = zdicSpiderMapper.selectZdicSpiderList(zdicSpider);
        list.forEach(item -> {
            item.setZi(new String(item.getZiBytes()));
            String kxzd = (String) redisTemplate.opsForHash().get("kxzd", item.getZi());
            String syjz = (String) redisTemplate.opsForHash().get("syjz", item.getZi());
            String gycd = (String) redisTemplate.opsForHash().get("gycd", item.getZi());
            item.setKxzdFmt(kxzd);
            item.setSyjzFmt(syjz);
            item.setGycdFmt(gycd);
        });
        return list;
    }

    /**
     * 新增汉典内容
     *
     * @param zdicSpider 汉典内容
     * @return 结果
     */
    @Override
    public int insertZdicSpider(ZdicSpider zdicSpider) {
        return zdicSpiderMapper.insertZdicSpider(zdicSpider);
    }

    /**
     * 修改汉典内容
     *
     * @param zdicSpider 汉典内容
     * @return 结果
     */
    @Override
    public int updateZdicSpider(ZdicSpider zdicSpider) {
        ZdicSpider item = this.selectZdicSpiderById(zdicSpider.getId());
        String zi = item.getZi();
        if (!StringUtils.isEmpty(zdicSpider.getGycdFmt())) {
            redisTemplate.opsForHash().put("gycd", zi, zdicSpider.getGycdFmt());
        }
        if (!StringUtils.isEmpty(zdicSpider.getSyjzFmt())) {
            redisTemplate.opsForHash().put("syjz", zi, zdicSpider.getSyjzFmt());
        }
        if (!StringUtils.isEmpty(zdicSpider.getKxzdFmt())) {
            redisTemplate.opsForHash().put("kxzd", zi, zdicSpider.getKxzdFmt());
        }
        return 1;
    }

    /**
     * 删除汉典内容对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteZdicSpiderByIds(String ids) {
        return zdicSpiderMapper.deleteZdicSpiderByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除汉典内容信息
     *
     * @param id 汉典内容ID
     * @return 结果
     */
    @Override
    public int deleteZdicSpiderById(Long id) {
        return zdicSpiderMapper.deleteZdicSpiderById(id);
    }
}
